﻿using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media;

namespace OpenLS.Spreadsheet.UI.Internals
{
    class ColorToTabBackgroundConverter: IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            if (values[0] == null || values[0] == DependencyProperty.UnsetValue)
                return Brushes.White;
            SpreadsheetColor color = StrictConverter.Convert<SpreadsheetColor>(values[0]);
            OpenLS.Drawing.ColorScheme colorScheme = StrictConverter.Convert<OpenLS.Drawing.ColorScheme>(values[1]);
            string hint = values[2] != null ? values[2].ToString() : string.Empty;
            var wpfColor = color.GetColor(colorScheme);
            switch (hint.ToLower())
            {
                case "selected":
                    return new LinearGradientBrush(
                        new GradientStopCollection(
                            new[]
                                {
                                    new System.Windows.Media.GradientStop {Color = Colors.White, Offset = 0},
                                    new System.Windows.Media.GradientStop {Color = Colors.White, Offset = 0.5},
                                    new System.Windows.Media.GradientStop {Color = MakeWhiter(wpfColor, 0.5), Offset = 1},
                                }), 90);
                case "hover":
                    return new LinearGradientBrush(
                        new GradientStopCollection(
                            new[]
                                {
                                    new System.Windows.Media.GradientStop {Color = MakeWhiter(wpfColor, 0.3), Offset = 0},
                                    new System.Windows.Media.GradientStop {Color = MakeWhiter(wpfColor, 0.6), Offset = 0.6},
                                    new System.Windows.Media.GradientStop {Color = MakeWhiter(wpfColor, 0.9), Offset = 1},
                                }), 90);
                default:
                    return new LinearGradientBrush(
                        new GradientStopCollection(
                            new[]
                                {
                                    new System.Windows.Media.GradientStop {Color = wpfColor, Offset = 0},
                                    new System.Windows.Media.GradientStop {Color = wpfColor, Offset = 1},
                                }), 90);
            }
        }

        private Color MakeWhiter(Color wpfColor, double factor)
        {
            return Color.FromRgb(MakeWhiter(wpfColor.R, factor), MakeWhiter(wpfColor.G, factor),
                                 MakeWhiter(wpfColor.B, factor));
        }
        byte MakeWhiter (Byte b, double factor)
        {
            return (byte) (b + (255 - b)*factor);
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotSupportedException();
        }
    }
}
